package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.AbnormalAttendance;
import com.amsp.oa.oademo.service.AbnormalAttendanceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/abnormal-attendance")
@Api(tags = "异常考勤接口")
public class AbnormalAttendanceController {

    @Autowired
    private AbnormalAttendanceService abnormalAttendanceService;

    @PostMapping("/save")
    @ApiOperation("保存异常考勤记录")
    public ResponseEntity<AbnormalAttendance> save(@RequestBody AbnormalAttendance abnormalAttendance) {
        if (abnormalAttendance.getId() != null) {
            abnormalAttendanceService.updateById(abnormalAttendance);
        } else {
            abnormalAttendanceService.save(abnormalAttendance);
        }
        return ResponseEntity.ok(abnormalAttendance);
    }

    @PostMapping("/batch-save")
    @ApiOperation("批量保存异常考勤记录")
    public ResponseEntity<List<AbnormalAttendance>> batchSave(@RequestBody List<AbnormalAttendance> records) {
        abnormalAttendanceService.saveOrUpdateBatch(records);
        return ResponseEntity.ok(records);
    }

    @GetMapping("/list")
    @ApiOperation("获取异常考勤记录列表")
    public ResponseEntity<IPage<AbnormalAttendance>> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        return ResponseEntity.ok(abnormalAttendanceService.getPage(current, size));
    }

    @GetMapping("/{id}")
    @ApiOperation("获取异常考勤记录详情")
    public ResponseEntity<AbnormalAttendance> getById(@PathVariable Long id) {
        return ResponseEntity.ok(abnormalAttendanceService.getById(id));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除异常考勤记录")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        abnormalAttendanceService.removeById(id);
        return ResponseEntity.ok().build();
    }
}